#include<stdio.h>      
#include<sys/types.h>      
#include<unistd.h>      
#include<stdlib.h>      
#include<sys/wait.h>     
#include<errno.h>
#include<string.h>
      
int main()      
{      
    pid_t id = fork();      
    if(id == 0)      
    {      
        // child      
        int cnt = 3;      
        while(cnt)      
        {      
            printf("我是一个子进程, pid: %d, ppid: %d\n", getpid(), getppid());      
            sleep(1);      
            cnt--;      
        }      
        exit(10);;      
    }      
       
    // parent          
    // 1. 子进程退出      
    // 2. 子进程没有退出呢? ?      
    //pid_t rid = wait(NULL);      
    int status = 0;
    pid_t rid = waitpid(id, &status, 0);
    if(rid > 0)      
    {      
        //printf("wait success, rid: %d, status: %d\n", rid, status);  
        //printf("wait success, rid: %d, exit code: %d\n", rid, (status>>8)&0xFF);      
        printf("wait success, rid: %d, exit code: %d, exit signal: %d\n", rid, WEXITSTATUS(status), status&0x7F);
    }      
    else
    {
        printf("wait failed: %d: %s\n", errno, strerror(errno));
    }
      
    return 0;      
}     
